﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Mvc;
using ServiceInterface.Model.Enum;
using Web.Infrastructure.Extensions;
using Web.Infrastructure.Helpers;
using Web.Infrastructure.Interface;
using Web.Infrastructure.Types;
using Web.InputModels;
using Web.Viewmodels;

namespace Web.Controllers
{
    public class GameController : Controller
    {
        [Route("")]
        [Route("Game")]
        [HttpGet]
        public async Task<ActionResult> NewGame()
        {
            var vm = new GamePropertiesViewModel
            {
                PlayerDifficulty = new Dictionary<string, string>()
                {
                    {"1", "1"},
                    {"2", "2"},
                    {"3", "3"},
                    {"4","4"},
                    {"5","5"},
                    {"6","6"},
                    {"7","7"},
                    {"8","8"},
                    {"9","9"},
                    {"10","10"}
                },
                Player1Color = UIEnums.Color.White,
                Player2Color = UIEnums.Color.Black
            };

            vm.Heuristics = HeuristicConstant.Heuristic1.EnumToDictionary(false);
            return View("NewGame", vm);
        }

        [HttpPost]
        [Route("Game/Create")]
        public async Task<ActionResult> CreateGame(GamePropertiesInputModel inputModel)
        {
            var response =  await AIInterface.CreateGame(MappingHelper.MapCreateGameInputModel(inputModel));
            
            var redirectUrl = new UrlHelper(Request.RequestContext).Action("Board", "Board", new
            {
                timeControl = (int)inputModel.TimeControl,
                player1Color = (int)inputModel.Player1Color,
                gameId = response.GameId,
                gameType = (int)inputModel.GameType
            });

            return Json(new { url = redirectUrl });
        }
    }
}